package com.maker.core;

import com.alibaba.fastjson.JSON;
import com.maker.config.WxPayProperties;
import com.maker.entity.Amount;
import com.maker.entity.NativePayParams;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class WxPayTemplate {

    private WxPayProperties wxPayProperties;
    private CloseableHttpClient httpClient;

    public WxPayTemplate(WxPayProperties wxPayProperties,CloseableHttpClient httpClient) {
        this.wxPayProperties = wxPayProperties;
        this.httpClient = httpClient;
    }

    public  String  createOrder(Integer total,String description,String outTradeNo) throws  Exception {
        String code_url = "";
//        Config config =
//                new RSAAutoCertificateConfig.Builder()
//                        .merchantId(merchantId)
//                        .privateKeyFromPath(privateKeyPath)
//                        .merchantSerialNumber(merchantSerialNumber)
//                        .apiV3Key(apiV3key)
//                        .build();
//        // 构建service
//        NativePayService service = new NativePayService.Builder().config(config).build();
//        // request.setXxx(val)设置所需参数，具体参数可见Request定义
//        PrepayRequest request = new PrepayRequest();

        HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native");
        Amount amount = Amount.builder().currency("CNY").total(total).build();
        NativePayParams nativePayParams = NativePayParams.builder().appid(wxPayProperties.getAppId())
                .mchid(wxPayProperties.getMchId())
                .description(description)
                .out_trade_no(outTradeNo)
                .notify_url("htpps://ssddsds")
                .amount(amount).build();
        String reqdata = JSON.toJSONString(nativePayParams);
        StringEntity entity = new StringEntity(reqdata, "utf-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept", "application/json");
        //完成签名并执行请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {//处理成功
                System.out.println("success,return body=" + EntityUtils.toString(response.getEntity()));
                code_url = JSON.parseObject(EntityUtils.toString(response.getEntity()), Map.class).get("code_url").toString();
            } else if (statusCode == 204) {//处理成功，无返回body
                System.out.println("success");
            } else {
                System.out.println("failed,resp code=" + statusCode + ",return body=" + EntityUtils.toString(response.getEntity()));
                throw new IOException("request faild");
            }
        } finally {
            response.close();
        }

        return code_url;
    }
}
